package com.xzl.xep.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.xzl.xep.common.utils.DateUtil;
import com.xzl.xep.common.utils.StringUtil;
import com.xzl.xep.dao.BusinessDao;
import com.xzl.xep.pojo.Customer;
import com.xzl.xep.pojo.Operator;
import com.xzl.xep.service.CustomerService;

@Service
public class CustomerServiceImpl extends BaseServiceImpl implements CustomerService {
	
	@Resource
	protected BusinessDao business;

	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(CustomerServiceImpl.class);
	
	@Override
	public List<Customer> getCustomers(Long companyId) {
		StringBuffer sb = new StringBuffer();
		
		sb.append("from Customer c where 1=1 ");
		if(companyId != 0)
			sb.append(" and c.companyId = ").append(companyId);
		
		sb.append(" order by modifyTime desc");
		
		return dao.selectByHql(sb.toString());
	}

	@Override
	public Double getTotalSpend(Long companyId) {
		StringBuffer sb = new StringBuffer();
		
		sb.append("select sum(c.totalSpend) from Customer c where 1=1 ");
		if(companyId != 0)
			sb.append(" and c.companyId = ").append(companyId);
		
		
		return dao.selectDoubleByHql(sb.toString());
	}

	@Override
	public Double getTotalCredit(Long companyId) {
	
		StringBuffer sb = new StringBuffer();
		
		sb.append("select sum(c.totalCredit) from Customer c where 1=1 ");
		if(companyId != 0)
			sb.append(" and c.companyId = ").append(companyId);
		
		return dao.selectDoubleByHql(sb.toString());
	}

	@Override
	public Customer addCustomer(Customer customer,Integer way,Operator operator) {
	
		customer.setCreateTime(DateUtil.getCurrentDate());
		
		customer.setTotalCredit(0d);
		customer.setTotalSpend(0d);
		customer.setModifyTime(DateUtil.getCurrentDate());
		customer.setOperatorId(operator.getId());
		customer.setOperatorName(operator.getRealName());
		
		if(StringUtil.isEmpty(customer.getName()))
			customer.setName(" ");
		
		if(StringUtil.isEmpty(customer.getMobile()))
			customer.setMobile(" ");
		
		Double money = customer.getBalance();
				
		customer.setBalance(0d);
		
		dao.saveObject(customer);
		
		if(money != null && money >0)
			business.credit(customer,money,way,operator);
		
		
		
		return customer;
	}
	
	@Override
	public boolean checkCustomerExists(Customer customer){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(*) from Customer c where companyId = ").append(customer.getCompanyId());
		sb.append(" and ( cardno = '").append(customer.getCardno()).append("'");
		
		if(StringUtil.isNotEmpty(customer.getMobile()))
			sb.append(" or mobile = '").append(customer.getMobile()).append("'");
		
		sb.append(" ) "); 
		
		if(dao.selectIntByHql(sb.toString()) >0 )
			return false;
		
		return true;
	}

	@Override
	public Integer getTotalPlus(Long companyId) {
		
		StringBuffer sb = new StringBuffer();
		
		sb.append("select sum(1) from Customer c where 1=1 ")
		  .append(" and createTime between '").append(DateUtil.getCurrentYMDstr()).append(" 00:00:00' ")
		  .append(" and '").append(DateUtil.getCurrentYMDstr()).append(" 23:59:59' ");
		if(companyId != 0)
			sb.append(" and c.companyId = ").append(companyId);
		
		return dao.selectIntByHql(sb.toString());
	}

	@Override
	public List<Customer> queryMobile(Long companyId,String mobile) {
		
		StringBuffer sb = new StringBuffer();
		
		sb.append("from Customer c where 1=1 ");
		if(companyId != 0)
			sb.append(" and c.companyId = ").append(companyId);
		sb.append(" and c.mobile like '%").append(mobile).append("%' ");
		sb.append(" order by mobile asc ");
		
		return dao.selectByHql(sb.toString(), 0, 10);
	}

	@Override
	public List<Customer> queryCardno(Long companyId, String cardno) {
		StringBuffer sb = new StringBuffer();
		
		sb.append("from Customer c where 1=1 ");
		if(companyId != 0)
			sb.append(" and c.companyId = ").append(companyId);
		sb.append(" and c.cardno like '%").append(cardno).append("%' ");
		sb.append(" order by cardno asc ");
		
		return dao.selectByHql(sb.toString(), 0, 10);
	}

	@Override
	public void credit(Operator operator, Long customerId, Integer way,
			Double money) {
		Customer customer = dao.loadObject(Customer.class, customerId);
		business.credit(customer,money,way,operator);
		
		
	}

}
